package com.cruise.game.controller.admin;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.param.admin.GameCategoryCreateParam;
import com.cruise.game.model.param.admin.GameCategoryUpdateParam;
import com.cruise.game.model.vo.admin.GameCategoryVO;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.service.admin.AdminCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理员游戏分类控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "管理员游戏分类相关接口")
@RestController
@RequestMapping("/api/admin/game-categories")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class AdminCategoryController extends BaseController {

    @Resource
    private AdminCategoryService adminCategoryService;

    /**
     * 获取游戏分类列表
     */
    @ApiOperation("获取游戏分类列表")
    @GetMapping
    public ApiResult<PageResult<GameCategoryVO>> getCategoryList(
            @ApiParam(value = "状态", required = false)
            @RequestParam(value = "status", required = false) String status,
            
            @ApiParam(value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            
            @ApiParam(value = "每页条数", required = false, defaultValue = "10")
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        
        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }
        
        return ApiResult.success(adminCategoryService.getCategoryList(status, page, pageSize));
    }

    /**
     * 获取游戏分类详情
     */
    @ApiOperation("获取游戏分类详情")
    @GetMapping("/{id}")
    public ApiResult<GameCategoryVO> getCategoryDetail(
            @ApiParam(value = "游戏分类ID", required = true)
            @PathVariable("id") Long id) {
        return ApiResult.success(adminCategoryService.getCategoryDetail(id));
    }

    /**
     * 创建游戏分类
     */
    @ApiOperation("创建游戏分类")
    @PostMapping
    public ApiResult<Map<String, Long>> createCategory(@Validated @RequestBody GameCategoryCreateParam param) {
        Long id = adminCategoryService.createCategory(param);
        Map<String, Long> result = new HashMap<>();
        result.put("id", id);
        return ApiResult.success("添加成功", result);
    }

    /**
     * 更新游戏分类
     */
    @ApiOperation("更新游戏分类")
    @PutMapping("/{id}")
    public ApiResult<Boolean> updateCategory(
            @ApiParam(value = "游戏分类ID", required = true)
            @PathVariable("id") Long id,
            
            @Validated @RequestBody GameCategoryUpdateParam param) {
        return ApiResult.success("更新成功", adminCategoryService.updateCategory(id, param));
    }

    /**
     * 删除游戏分类
     */
    @ApiOperation("删除游戏分类")
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> deleteCategory(
            @ApiParam(value = "游戏分类ID", required = true)
            @PathVariable("id") Long id) {
        return ApiResult.success("删除成功", adminCategoryService.deleteCategory(id));
    }

    /**
     * 更新游戏分类状态
     */
    @ApiOperation("更新游戏分类状态")
    @PutMapping("/{id}/status")
    public ApiResult<Boolean> updateCategoryStatus(
            @ApiParam(value = "游戏分类ID", required = true)
            @PathVariable("id") Long id,
            
            @ApiParam(value = "状态", required = true)
            @RequestParam("status") String status) {
        return ApiResult.success("状态已更新", adminCategoryService.updateCategoryStatus(id, status));
    }
}
